package com.bigkoo.pickerview.utils;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.R;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.List;
import java.util.Observable;

public class WheelPickerUtil {

    private WheelPickerUtil() {}

    public static class WheelPickerUtilViewHolder {
        public static final WheelPickerUtil instance = new WheelPickerUtil();
    }

    public static WheelPickerUtil getInstance() {
        return WheelPickerUtilViewHolder.instance;
    }

    private OptionsPickerView mOptionsPickerView;

    /**
     * 滚动弹窗
     * @param context
     * @param list
     */
    public void showOptionsAlert(Context context, final List list, final OnOptionsListener listener) {
        mOptionsPickerView = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                listener.select(options1, list.get(options1));
            }
        }).setLayoutRes(R.layout.pickerview_custom_options_item, new CustomListener() {
            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                final TextView tvCancel = v.findViewById(R.id.tvCancel);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOptionsPickerView.returnData();
                        mOptionsPickerView.dismiss();
                    }
                });
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOptionsPickerView.dismiss();
                    }
                });
            }
        }).isDialog(true).setOutSideCancelable(false).build();
        mOptionsPickerView.setPicker(list);//添加数据
        mOptionsPickerView.show();
    }

    public interface OnOptionsListener {
        void select(int position, Object data);
    }
}
